/* 
 * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
 * Copyright (c) 2019 Alejandro "alkeon" Castilla
 * 
 * This program is free software: you can redistribute it and/or modify  
 * it under the terms of the GNU General Public License as published by  
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include "SDL.h"
#include "slider.h"
#include "song.h"
#define WHITE 255,255,255,255
using namespace std;

SDL_Point mouse;
bool mouse_follow = false;
SDL_Point mouse_offset;

int auxiliar_songs = 0;

#define MAX_AUX_VALUE 2

int main(){

	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		cout << "SDL not working " << SDL_GetError() << endl;
	
	SDL_Window * gWindow = SDL_CreateWindow("Soft mood", SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED, 700, 480, 0);
	if(gWindow == NULL){
		cout << "Render did not start" << SDL_GetError() << endl;
		return 1;
	}

	SDL_Renderer * gRenderer = SDL_CreateRenderer(gWindow, -1,
		SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if(gRenderer == NULL){
		cout << "Render did not start" << SDL_GetError() << endl;
		return 1;
	}
	
	SDL_Event event;
	if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1){
		cout << "Audio did not start " << SDL_GetError() << endl;
		return 1;
	}

	int flags = 0;
	int init = Mix_Init(flags);
	if((init&flags) != flags) {
		printf("Mix_Init: Failed to init Mixer!\n");
		printf("Mix_Init: %s\n", Mix_GetError());

	}

	string auxiliar_songs_name[] = {
		"sound/heartbeat.wav", 
		"sound/fountain.wav" 	// Madrid fountain. Awesome :)
	};
	string auxiliar_images_name[] = {"icons/heart.png", "icons/fountain.png"};
	int running = 1;

	/* Start loading screen */

	SDL_SetRenderDrawColor(gRenderer, WHITE);
	SDL_RenderClear(gRenderer);

	SDL_Rect s1;
	s1.x = 286;
	s1.y = 176;
	
	SDL_Surface * temp_surface;
	temp_surface = IMG_Load("icons/clock.png");
	if( temp_surface == NULL ){
		cout << "Icon (icons/clock.png) couldn't be loaded" << endl;
		return 1;
	}

	SDL_Texture * fillbar = SDL_CreateTextureFromSurface(gRenderer, temp_surface);
	if( fillbar == NULL ){
		cout << "Fillbar couldn't be created" << endl;
		return 1;
	}

	SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
	SDL_RenderCopy(gRenderer, fillbar, NULL, &s1);
	SDL_DestroyTexture(fillbar);
	SDL_FreeSurface(temp_surface);
	SDL_RenderPresent(gRenderer);

	/* Load audio on background */

	song rain("sound/rain.wav");
	song thunder("sound/thunder.wav");
	song waves("sound/waves.wav");
	song wind("sound/wind.wav");
	song fire("sound/fire.wav");
	song birds("sound/birds.wav");
	song crickets("sound/crickets.wav");
	song auxiliar(auxiliar_songs_name[0]);

	/* End load screen */

	SDL_SetRenderDrawColor(gRenderer, WHITE);
	SDL_RenderClear(gRenderer);

	/* Start sliders and icons */

	slider s_rain(gRenderer, 50, 300);
	slider s_thunder(gRenderer, 100, 300);
	slider s_waves(gRenderer, 150, 300);
	slider s_wind(gRenderer, 200, 300);
	slider s_fire(gRenderer, 250, 300);
	slider s_birds(gRenderer, 300, 300);
	slider s_crickets(gRenderer, 350, 300);
	slider s_auxiliar(gRenderer, 400, 300);

	s_rain.set_image("icons/rain.png");
	s_thunder.set_image("icons/flash.png");
	s_waves.set_image("icons/waves.png");
	s_wind.set_image("icons/wind.png");
	s_fire.set_image("icons/fire.png");
	s_birds.set_image("icons/bird.png");
	s_crickets.set_image("icons/cricket.png"); // cricket player icon but sound is cricket bug
	s_auxiliar.set_image("icons/heart.png");

	SDL_RenderPresent(gRenderer);

	bool minimized = false;

	while(running){
	
		SDL_GetMouseState(&mouse.x, &mouse.y);
		while(SDL_PollEvent(&event) != 0){

			if(event.type == SDL_QUIT)
				running = 0;

			if(event.button.button == SDL_BUTTON_LEFT){

				if(event.type == SDL_MOUSEBUTTONDOWN){

					mouse_follow = true;
					s_rain.test_mouse_follow(&mouse,&mouse_offset);
					s_thunder.test_mouse_follow(&mouse,&mouse_offset);
					s_waves.test_mouse_follow(&mouse,&mouse_offset);
					s_wind.test_mouse_follow(&mouse,&mouse_offset);
					s_fire.test_mouse_follow(&mouse,&mouse_offset);
					s_birds.test_mouse_follow(&mouse,&mouse_offset);
					s_crickets.test_mouse_follow(&mouse,&mouse_offset);
					s_auxiliar.test_mouse_follow(&mouse,&mouse_offset);

				}

				if(event.type == SDL_MOUSEBUTTONUP){

					mouse_follow = false;
					s_rain.mouse_follow_off();
					s_thunder.mouse_follow_off();
					s_waves.mouse_follow_off();
					s_wind.mouse_follow_off();
					s_fire.mouse_follow_off();
					s_birds.mouse_follow_off();
					s_crickets.mouse_follow_off();
					s_auxiliar.mouse_follow_off();

				}
			}	

			if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESTORED){
				minimized = false;
				SDL_SetRenderDrawColor(gRenderer, WHITE);
				SDL_RenderClear(gRenderer);
				s_rain.draw();
				s_thunder.draw();
				s_waves.draw();
				s_wind.draw();
				s_fire.draw();
				s_birds.draw();
				s_crickets.draw();
				s_auxiliar.draw();
				SDL_RenderPresent(gRenderer);
			}

			if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_MINIMIZED)
				minimized = true;

			if(event.type == SDL_KEYDOWN){

				switch(event.key.keysym.sym){
					case SDLK_ESCAPE: running = 0; break;
					case SDLK_m:{
						++auxiliar_songs;
						auxiliar_songs = auxiliar_songs % MAX_AUX_VALUE;
						SDL_RenderClear(gRenderer);
						auxiliar.change_song(auxiliar_songs_name[auxiliar_songs], s_auxiliar.get_value());
						s_auxiliar.set_image(auxiliar_images_name[auxiliar_songs]);
						s_rain.draw();
						s_thunder.draw();
						s_waves.draw();
						s_wind.draw();
						s_fire.draw();
						s_birds.draw();
						s_crickets.draw();
						s_auxiliar.draw();
						SDL_RenderPresent(gRenderer);
						SDL_Delay(10);
					};break;
					default: break;
				}
			}
		}

		if(!minimized){
			if(mouse_follow){
				SDL_RenderClear(gRenderer);
		
				s_rain.update(mouse.y-mouse_offset.y);
				s_thunder.update(mouse.y-mouse_offset.y);
				s_waves.update(mouse.y-mouse_offset.y);
				s_wind.update(mouse.y-mouse_offset.y);
				s_fire.update(mouse.y-mouse_offset.y);
				s_birds.update(mouse.y-mouse_offset.y);
				s_crickets.update(mouse.y-mouse_offset.y);
				s_auxiliar.update(mouse.y-mouse_offset.y);

				s_rain.draw();
				s_thunder.draw();
				s_waves.draw();
				s_wind.draw();
				s_fire.draw();
				s_birds.draw();
				s_crickets.draw();
				s_auxiliar.draw();

				rain.set_volume(s_rain.get_value());
				thunder.set_volume(s_thunder.get_value());
				waves.set_volume(s_waves.get_value());
				wind.set_volume(s_wind.get_value());
				fire.set_volume(s_fire.get_value());
				birds.set_volume(s_birds.get_value());
				crickets.set_volume(s_crickets.get_value());
				auxiliar.set_volume(s_auxiliar.get_value());
				
				SDL_RenderPresent(gRenderer);
				SDL_Delay(10);

			}else
				SDL_Delay(50);

		}else{
		
			SDL_WaitEvent(&event);
			minimized = false;

		}
	}	
}
